﻿using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.UserCotrols
{
    public partial class CtrlYearFunds : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string busId = Request.QueryString["BusinessId"].ToString();

                hidId.Value = busId;
                ViewState["busId"] = busId.ToString();
                BindDt(busId);
            }
        }

        #region 旧代码

        //        /// <summary>
        //        /// 绑定数据
        //        /// </summary>
        //        public void BindDt(string busId)
        //        {
        //            QS_Business bModel = new Qs_BusinessBLL().GetBusinessById(Decimal.Parse(busId));

        //            lblTime.Text = bModel.Business_operator_date.ToShortDateString();

        //            lblUser.Text = new Sys_UserBLL().GetInfoById(bModel.Business_operator_Id.ToString()).USER_NAME;

        //            string sql = string.Format(@" select * from qx_planyearapply t where t.n_business_id=" + busId);

        //            DataTable dtYear = new BaseBLL().Query(sql);

        //            if (dtYear == null || dtYear.Rows.Count < 1)
        //            {
        //                Response.Write("<script>alert('【系统提示】该条数据有误，请与管理员联系');location.href='../Right.aspx';</script>");
        //                return;
        //            }

        //            lblyear.Text = dtYear.Rows[0]["N_YEAR"].ToString();

        //            lblBzyj.Text = dtYear.Rows[0]["C_REAMRK"].ToString();

        //            lbldw.Text = new Sys_CompanyBLL().GetInfoById(dtYear.Rows[0]["N_COMPANY_ID"].ToString()).CMY_NAME;

        //            this.hidId.Value = dtYear.Rows[0]["N_YEARAPP_ID"].ToString() + "@" + busId;

        //            DataTable dt = new QX_PLANYEARAPPLYBLL().GetDataBind(busId);

        //            gvMonthFunds.DataSource = dt;

        //            gvMonthFunds.DataBind();
        //        }

        //        /// <summary>
        //        /// 返回
        //        /// </summary>
        //        /// <param name="sender"></param>
        //        /// <param name="e"></param>
        //        protected void btnReturn_Click(object sender, EventArgs e)
        //        {
        //            string url = this.Parent.Page.ToString();
        //            if (url == "ASP.businessexpansion_business_businformation_aspx")
        //            {
        //                Response.Redirect("../../UserCotrol/ProcessCotrol/MyIndexEx.aspx");
        //                Response.End();
        //            }
        //            if (url == "ASP.businessexpansion_business_detailed_aspx")
        //            {
        //                Response.Redirect("../../BusinessExpansion/Right.aspx");
        //                Response.End();
        //            }
        //            if (url == "ASP.businessexpansion_business_updataapplydetail_aspx")
        //            {
        //                Response.Redirect("../../BusinessExpansion/Business/ApplyInfor.aspx");
        //                Response.End();
        //            }
        //        }
        //        protected void btnEdit_Click(object sender, EventArgs e)
        //        {
        //            Response.Redirect("../../BusinessExpansion/Business/MonthPlusUnlock.aspx?BusinessId=" + ViewState["busId"].ToString());

        //        }
        //        protected void btnPlus_Unlock_Click(object sender, EventArgs e)
        //        {
        //            int state = 0;
        //            if (btnPlus_Unlock.Text == "解锁")
        //            {
        //                if (ViewState["EditAuthority"].ToString().ToLower() == "true")
        //                {
        //                    btnEdit.Visible = true;
        //                }
        //                state = 0;
        //                btnPlus_Unlock.Text = "未锁定";
        //            }
        //            else
        //            {
        //                state = 1;
        //                btnPlus_Unlock.Text = "解锁";
        //                btnEdit.Visible = false;
        //            }
        //            string sql = "Update qs_monthplan set islock=" + state + " where n_business_id=" + ViewState["busId"].ToString();
        //            int count = new BaseBLL().NoQuery(sql, "");
        //        }

        //        protected void gvMonthFunds_RowDataBound(object sender, GridViewRowEventArgs e)
        //        {
        //            if (e.Row.RowType == DataControlRowType.DataRow)
        //            {
        //                string type = ((Label)e.Row.FindControl("lbltype")).Text;
        //                if (type == "0")
        //                {
        //                    e.Row.Attributes["style"] = "background-color:#B2D8FF;font-size: 14px";

        //                    // e.Row.Cells[2].Attributes["style"] = "font-size: 14px";
        //                }
        //                else if (type == "1")
        //                {
        //                    e.Row.Attributes["style"] = "background-color:#CCCCCC;font-size: 13px;";

        //                    //  e.Row.Cells[2].Attributes["style"] = "font-size: 13px";
        //                }

        //            }
        //        }

        //        /// <summary>
        //        /// 打印事件
        //        /// </summary>
        //        /// <param name="sender"></param>
        //        /// <param name="e"></param>
        //        protected void PrintClick(object sender, EventArgs e)
        //        {
        //            try
        //            {
        //                decimal id = Convert.ToDecimal(ViewState["busId"]);
        //                var entity = new QX_PLANYEARAPPLYBLL().FindByBuid(id);
        //                //var user = userBLL.GetInfoById(entity.N_USER_ID.ToString());
        //                Session["YEAREDITFUNDSHeader"] = entity.N_YEAR + "年计划申请";
        //                var sql = @"SELECT
        //	T1.N_YDETAIL_ID AS 详情编号,
        //	T1.N_PROID AS 项目编号,
        //	T1.C_PRONAME AS 项目名称,
        //	T1.N_AMOUNT AS 申请金额,
        //	DECODE(T1.N_TYPE,0,'主节点',1,'分节点',2,'子节点（可录入数据）','其它') AS 节点类型,
        //	T1.C_REMARK AS 备注
        //FROM QX_PLANYEAR_DETAIL T1
        //LEFT JOIN QS_BUSINESS T2
        //ON T1.N_BUSINESS_ID = T2.N_BUSINESS_ID
        //WHERE T1.N_BUSINESS_ID = {0}
        //ORDER BY T1.N_PROID
        //";
        //                sql = string.Format(sql, id);
        //                var dt = new QX_PLANYEARAPPLYBLL().Query(sql);
        //                Session["YEAREDITFUNDSBody"] = dt;
        //                MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=BASIC&VALUE=YEAREDITFUNDS\",Area:[\"800px\",\"500px\"],Scrollbar:true})");
        //            }
        //            catch (Exception)
        //            {
        //                MessageBox.Show(Page, "打印异常");
        //            }

        //        }

        #endregion 旧代码

        #region 新代码

        public void BindDt(string busId)
        {
            QS_Business bModel = new Qs_BusinessBLL().GetBusinessById(Decimal.Parse(busId));

            lblTime.Text = bModel.Business_operator_date.ToShortDateString();

            lblUser.Text = new Sys_UserBLL().GetInfoById(bModel.Business_operator_Id.ToString()).USER_NAME;

            string sql = string.Format(@" select * from qx_planyearapply t where t.n_business_id=" + busId);

            DataTable dtYear = new BaseBLL().Query(sql);

            if (dtYear == null || dtYear.Rows.Count < 1)
            {
                Response.Write("<script>alert('【系统提示】该条数据有误，请与管理员联系');location.href='../Right.aspx';</script>");
                return;
            }

            lblyear.Text = dtYear.Rows[0]["N_YEAR"].ToString();

            lblBzyj.Text = dtYear.Rows[0]["C_REAMRK"].ToString();

            lbldw.Text = new Sys_CompanyBLL().GetInfoById(dtYear.Rows[0]["N_COMPANY_ID"].ToString()).CMY_NAME;

            this.hidId.Value = dtYear.Rows[0]["N_YEARAPP_ID"].ToString() + "@" + busId;

            DataTable dt = new QX_PLANYEARAPPLYBLL().GetDataBind(busId);

            gvMonthFunds.DataSource = dt;

            gvMonthFunds.DataBind();
        }

        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string url = this.Parent.Page.ToString();
            if (url == "ASP.businessexpansion_business_businformation_aspx")
            {
                Response.Redirect("../../UserCotrol/ProcessCotrol/MyIndexEx.aspx");
                Response.End();
            }
            if (url == "ASP.businessexpansion_business_detailed_aspx")
            {
                Response.Redirect("../../BusinessExpansion/Right.aspx");
                Response.End();
            }
            if (url == "ASP.businessexpansion_business_updataapplydetail_aspx")
            {
                Response.Redirect("../../BusinessExpansion/Business/ApplyInfor.aspx");
                Response.End();
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect("../../BusinessExpansion/Business/MonthPlusUnlock.aspx?BusinessId=" + ViewState["busId"].ToString());
        }

        protected void btnPlus_Unlock_Click(object sender, EventArgs e)
        {
            int state = 0;
            if (btnPlus_Unlock.Text == "解锁")
            {
                if (ViewState["EditAuthority"].ToString().ToLower() == "true")
                {
                    btnEdit.Visible = true;
                }
                state = 0;
                btnPlus_Unlock.Text = "未锁定";
            }
            else
            {
                state = 1;
                btnPlus_Unlock.Text = "解锁";
                btnEdit.Visible = false;
            }
            string sql = "Update qs_monthplan set islock=" + state + " where n_business_id=" + ViewState["busId"].ToString();
            int count = new BaseBLL().NoQuery(sql, "");
        }

        protected void gvMonthFunds_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string type = ((Label)e.Row.FindControl("lbltype")).Text;
                if (type == "0")
                {
                    e.Row.Attributes["style"] = "background-color:#B2D8FF;font-size: 14px";

                    // e.Row.Cells[2].Attributes["style"] = "font-size: 14px";
                }
                else if (type == "1")
                {
                    e.Row.Attributes["style"] = "background-color:#CCCCCC;font-size: 13px;";

                    //  e.Row.Cells[2].Attributes["style"] = "font-size: 13px";
                }
            }
        }

        /// <summary>
        /// 打印事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PrintClick(object sender, EventArgs e)
        {
            try
            {
                decimal id = Convert.ToDecimal(ViewState["busId"]);
                var entity = new QX_PLANYEARAPPLYBLL().FindByBuid(id);
                //var user = userBLL.GetInfoById(entity.N_USER_ID.ToString());
                Session["YEAREDITFUNDSHeader"] = entity.N_YEAR + "年计划申请";
                var sql = @"SELECT
	T1.N_YDETAIL_ID AS 详情编号,
	T1.N_PROID AS 项目编号,
	T1.C_PRONAME AS 项目名称,
	T1.N_AMOUNT AS 申请金额,
	(CASE WHEN T1.N_TYPE = 0 THEN '主节点' WHEN T1.N_TYPE = 1 THEN '分节点' WHEN T1.N_TYPE = 2 THEN '子节点（可录入数据）' ELSE '其它' END) AS 节点类型,
	T1.C_REMARK AS 备注
FROM QX_PLANYEAR_DETAIL T1
LEFT JOIN QS_BUSINESS T2
ON T1.N_BUSINESS_ID = T2.N_BUSINESS_ID
WHERE T1.N_BUSINESS_ID = {0}
ORDER BY T1.N_PROID
";
                sql = string.Format(sql, id);
                var dt = new QX_PLANYEARAPPLYBLL().Query(sql);
                Session["YEAREDITFUNDSBody"] = dt;
                MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=BASIC&VALUE=YEAREDITFUNDS\",Area:[\"800px\",\"500px\"],Scrollbar:true})");
            }
            catch (Exception)
            {
                MessageBox.Show(Page, "打印异常");
            }
        }

        #endregion 新代码
    }
}